class Solution {
public:
    int lowerbound(vector<int> &prefix,int x)
    {
        int left=0,right=prefix.size()-1;
        while(left<right)
        {
            int mid=(left+right+1)>>1;
            if(prefix[mid]<=x) left=mid;
            else right=mid-1;
        }
        return left;
    }

    vector<int> answerQueries(vector<int>& nums, vector<int>& queries) {
        const int N=1010;
        sort(nums.begin(),nums.end());
        int n=nums.size();
        vector<int> prefix(n+1);
        for(int i=0;i<n;++i)
        {
            prefix[i+1]=prefix[i]+nums[i];
        }
        int m=queries.size();
        vector<int> ret(m);
        for(int i=0;i<m;++i)
        {
            int p=lowerbound(prefix,queries[i]);
            if(prefix[p]<=queries[i]) ret[i]=p;
        }
        return ret;
    }
};
